package com.kedacom.general.netty.handler;

import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@ChannelHandler.Sharable
public class UdpProcessHanlder extends SimpleChannelInboundHandler<DatagramPacket> {

    @Autowired
    KafkaTemplate kafkaTemplate;

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    /**
     *
     * @param channelHandlerContext
     * @param datagramPacket
     */
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) {
        log.warn("接收数据");
        ByteBuf byteBuf = datagramPacket.content();
        byte[] barray = new byte[byteBuf.readableBytes()];
        byteBuf.getBytes(0, barray);
        log.warn("udp发送数据:"+new String(barray));
        String shownNumber = "000000012345";

//        Object obj = redisTemplate.opsForHash().get("interphone_gbid_Hash", shownNumber);
//        if(obj==null){
//            return;
//        }
//        String latitude = "36";
//        String longitude = "123";
//        String timeStamp = "1653799499000";
//        Double speed = 0.0;
//        Double direction = 0.0;
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("latitude", latitude);
//        jsonObject.put("longitude", longitude);
//        jsonObject.put("devGbId", obj.toString());
//        jsonObject.put("direction", direction);
//        jsonObject.put("speed", speed);
//        jsonObject.put("timestamp", timeStamp);
//        kafkaTemplate.send("DEVICE_GPS", jsonObject.toString());
    }
}
